<?php

namespace App\Http\Controllers\Api\V1\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\YaoDivination;

class UserDivinationRecordItemResource extends JsonResource
{
    public function toArray($request)
    {
    	$resource = $this->resource;
    	
        $categoryId = request()->category_id;

        $content = "";
        $beng = "";
        $by = "";
        $biang = "";
        if($categoryId){
            $res = $resource->divinationCategoriesRelations()->where('category_id',$categoryId)->first();
            if($res){
                $content =$res->content;
                $yaoContent = YaoDivination::find($res->yao_divination_id);
                $beng = $yaoContent->divination->title;
                $biang = $yaoContent->changeDivination->title;
                $by = config("site.bianyao")[$yaoContent->yao_count];
            }

        }
        $divination = $resource->is_show ==1 ? new divinationItemResource($resource->divination) : new divinationItemNoShowResource($resource->divination);

        $scenicDivination = "";
        if($resource->scenic_area_id){

            $scenicDivination = new divinationYzyItemResource($resource->divination);
        }
        return [
        	'id' => (int)$resource->id,
	        'divination'=>$divination,
            'scenicDivination'=>$scenicDivination,
            'is_show'=>$resource->is_show,
            'cause'=>$resource->cause,
            'content'=>$content,
            'beng'=>$beng,
            'by'=>$by,
            'biang'=>$biang,
	        'created_at' => (string)$resource->created_at,
        ];
    }
}
